package com.cake.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.cake.base.config.Constant;
import com.cake.base.util.Base64FileUtils;
import com.cake.base.util.PropertiesUtils;
import com.cake.dao.XxProductMapper;
import com.cake.dao.XxProductProductProductImageMapper;
import com.cake.dao.XxProductSpecificationMapper;
import com.cake.dao.XxProductSpecificationValueMapper;
import com.cake.dao.XxSpecificationMapper;
import com.cake.dao.XxSpecificationValueMapper;
import com.cake.entity.XxProduct;
import com.cake.entity.XxProductProductImage;
import com.cake.entity.XxProductSpecificationKey;
import com.cake.entity.XxProductSpecificationValueKey;
import com.cake.entity.XxSpecification;
import com.cake.entity.XxSpecificationValue;
import com.cake.entity.pojo.Base64Bean;
import com.cake.entity.pojo.ProductPoJo;
import com.cake.entity.pojo.SpecificationValuePojo;
import com.cake.service.base.BaseService;
import com.cake.service.inter.IXxProductService;

/**
 * 商品业务层
 * @author Administrator
 *
 */
@Service
@Transactional
public class XxProductServiceImpl extends BaseService<XxProduct> implements IXxProductService{

	@Autowired
	private XxProductMapper xxProductMapper;
	
	@Autowired
	private XxSpecificationValueMapper xxSpecificationValueMapper;
	
	@Autowired
	private XxProductSpecificationMapper xxProductSpecificationMapper;
	
	@Autowired
	private XxProductProductProductImageMapper xxProductProductProductImageMapper;
	
	@Autowired
	private XxSpecificationMapper xxSpecificationMapper;
	
	@Autowired
	private XxProductSpecificationValueMapper xxProductSpecificationValueMapper;
	
	@Override
	public List<XxProduct> selectProductInfos(XxProduct xxProduct) {
		return xxProductMapper.selectProductInfos(xxProduct);
	}

	@Override
	public ProductPoJo selectProductDetails(Long productId) {
		ProductPoJo productPoJo = xxProductMapper.selectProductDetails(productId);
		productPoJo.setSpecificationValues(xxSpecificationValueMapper.selectSpecificationValuesByProductId(productPoJo.getProductId()));
		productPoJo.setProductImages(xxProductProductProductImageMapper.selectProductImagesByProductId(productPoJo.getProductId()));
		return productPoJo;
	}

	@Override
	public Integer insertOrUpdateProductAndSpecifications(ProductPoJo productPoJo) throws Exception {
		XxProduct xxProduct = new XxProduct();
		xxProduct.setId(productPoJo.getProductId());
		xxProduct.setName(productPoJo.getName());
		xxProduct.setFullName(productPoJo.getName());
		xxProduct.setEnglishName(productPoJo.getEnglishName());
		xxProduct.setIntroduction(productPoJo.getProductDetailsIntroduction());
		xxProduct.setPrice(productPoJo.getSpecificationValues().get(0).getProductPrice());
		xxProduct.setCost(productPoJo.getCost());
		xxProduct.setMarketPrice(productPoJo.getSpecificationValues().get(0).getProductPrice());
		xxProduct.setFoodStorage(productPoJo.getFoodStorage());
		xxProduct.setAnnouncement(productPoJo.getAnnouncement());
		xxProduct.setIsCake(productPoJo.getIsCake() == 1);
		xxProduct.setSendIntro(productPoJo.getSendIntro());
		xxProduct.setSendTimeIntro(productPoJo.getSendTimeIntro());
		xxProduct.setFoodStorage(productPoJo.getFoodStorage());
		xxProduct.setProductCategory(productPoJo.getProductCategory());
		xxProduct.setCreateDate(new Date());
		xxProduct.setSn(StringUtils.isEmpty(productPoJo.getSn()) ? new Date().getTime() + "" :  productPoJo.getSn());
		xxProduct.setPoint(productPoJo.getPoint());
		xxProduct.setIsMarketable(productPoJo.getIsMarketableFlag() == 1);
		int productCode = 0;
		if(xxProduct.getId() == null){
			productCode = xxProductMapper.insertSelective(xxProduct);
		}else{
			productCode = xxProductMapper.updateByPrimaryKeySelective(xxProduct);
		}
		if(productCode > 0){
		    
			xxProductProductProductImageMapper.deleteProductImagesByProductId(xxProduct.getId());
			
			// 处理商品banner
			List<Base64Bean> base64ImageBeans = productPoJo.getBase64ImageBeans();
			for (Base64Bean base64Bean : base64ImageBeans) {
		        // 保存其他图片为滚动图片
		        this.insertProductImage(xxProduct.getId(), base64Bean.getBase64String());
			}
			
			// 处理缩略图
            String image = productPoJo.getThumbnailBase64Image();
            if(StringUtils.hasText(image))
                xxProduct.setImage(uploadImage(image));
			
			xxProductMapper.updateByPrimaryKey(xxProduct);
			XxSpecification xxSpecification = new XxSpecification();
			xxSpecification.setId(productPoJo.getSpecificationId());
			xxSpecification.setName(productPoJo.getProductUnitName());
			if(xxSpecification.getId() == null){
				xxSpecification.setCreateDate(new Date());
				xxSpecificationMapper.insert(xxSpecification);
			}else{
				xxSpecificationMapper.updateByPrimaryKeySelective(xxSpecification);
			}
			XxProductSpecificationKey xxProductSpecificationKey  = new XxProductSpecificationKey();
			xxProductSpecificationKey.setProducts(xxProduct.getId());
			xxProductSpecificationKey.setSpecifications(xxSpecification.getId());
			if(xxProductSpecificationMapper.selectByPrimaryTwoKey(xxProductSpecificationKey) == null){
				xxProductSpecificationMapper.insert(xxProductSpecificationKey);
			}
			List<SpecificationValuePojo> specificationValues = productPoJo.getSpecificationValues();
			xxProductSpecificationValueMapper.deleteByProductId(productPoJo.getProductId());
			for (SpecificationValuePojo specificationValuePojo : specificationValues) {
				XxSpecificationValue xxSpecificationValue = new XxSpecificationValue();
				xxSpecificationValue.setId(specificationValuePojo.getSpecificationValueId());
				xxSpecificationValue.setName(specificationValuePojo.getProductUnitNumber());
				xxSpecificationValue.setSize(specificationValuePojo.getProductSize());
				xxSpecificationValue.setIntroduction(specificationValuePojo.getProductSpecificationIntroduction());
				xxSpecificationValue.setPrice(specificationValuePojo.getProductPrice());
				xxSpecificationValue.setSpecification(productPoJo.getSpecificationId());
				this.handleXxSpecificationValue(xxSpecificationValue);
				if(specificationValuePojo.getSpecificationValueId() == null){
					xxSpecificationValue.setCreateDate(new Date());
					xxSpecificationValue.setSpecification(xxSpecification.getId());
					xxSpecificationValueMapper.insert(xxSpecificationValue);
				}else{
					xxSpecificationValueMapper.updateByPrimaryKeySelective(xxSpecificationValue);
				}
				XxProductSpecificationValueKey xxProductSpecificationValueKey = new XxProductSpecificationValueKey();
				xxProductSpecificationValueKey.setProducts(xxProduct.getId());
				xxProductSpecificationValueKey.setSpecificationValues(xxSpecificationValue.getId());
				xxProductSpecificationValueMapper.insert(xxProductSpecificationValueKey);
			}
		}
		return productCode;
	}

	@Override
	public String insertProductImage(Long productId, String base64String) throws Exception {
		String imageUrl = uploadImage(base64String);
		XxProductProductImage xxProductProductImage = new XxProductProductImage();
		xxProductProductImage.setProduct(productId);
		xxProductProductImage.setThumbnail(imageUrl);
		xxProductProductImage.setSource(imageUrl);
		xxProductProductProductImageMapper.insert(xxProductProductImage);
		return imageUrl;
	}

    /**
     * 上传图片
     * @param base64String
     * @return
     * @throws Exception
     */
    private String uploadImage(String base64String) throws Exception {
        String imageUrl = Base64FileUtils.base64ToImageFile(base64String, 
				PropertiesUtils.getProperties(Constant.SERVER_FILE_IMAGE_PHYSICAL_URL_PRE)
				, UUID.randomUUID().toString() + ".png");
        return imageUrl;
    }
	
	private XxSpecificationValue handleXxSpecificationValue(XxSpecificationValue xxSpecificationValue){
		switch (xxSpecificationValue.getName()) {
		case "4":
			xxSpecificationValue.setAttributeValue2("6");
			xxSpecificationValue.setAttributeValue1("送2套餐具");
			break;
		case "6":
			xxSpecificationValue.setAttributeValue2("6");
			xxSpecificationValue.setAttributeValue1("送5套餐具");
			break;
		case "8":
			xxSpecificationValue.setAttributeValue2("8");
			xxSpecificationValue.setAttributeValue1("送10套餐具");
			break;
		default:
			xxSpecificationValue.setAttributeValue2("8");
			xxSpecificationValue.setAttributeValue1("送12套餐具");
			break;
		}
		return xxSpecificationValue;
	}

	@Override
	public List<XxProduct> selectProductsByCategory(Long productCategoryId) {
		return xxProductMapper.selectProductsByCategory(productCategoryId);
	}
	
}
